import java.util.*;
import java.util.stream.Collectors;

public class EndMethodDemo {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>(Arrays.asList("张三丰", "张三丰", "张无忌", "张翠山", "王二麻子", "张良", "谢广坤"));
        // forEach,对此流的每个元素执行操作
        System.out.println("===================================");
        list1.stream().filter(s -> s.startsWith("张")).forEach(System.out::println);

        // count,返回此流中的元素数
        System.out.println("===================================");
        System.out.println(list1.stream().filter(s -> s.startsWith("张")).count());

        // toList,使用List集合收集数据
        System.out.println("===================================");
        List<String> list2 = list1.stream().filter(s -> s.startsWith("张")).collect(Collectors.toList());
        System.out.println(list2);

        // toSet,使用Set集合收集数据
        System.out.println("===================================");
        Set<String> set = list1.stream().filter(s -> s.startsWith("张")).collect(Collectors.toSet());
        System.out.println(set);

        // toMap,使用Map集合收集数据
        System.out.println("===================================");
        ArrayList<String> list = new ArrayList<>();
        list.add("zhangsan,23");
        list.add("lisi,24");
        list.add("wangwu,25");
        Map<String, Integer> collect = list.stream().filter(i -> {
            String[] split = i.split(",");
            int age = Integer.parseInt(split[1]);
            return age >= 24;
        }).collect(Collectors.toMap(
                i -> i.split(",")[0],
                i -> Integer.parseInt(i.split(",")[1])
        ));
        System.out.println(collect);
    }
}
